clear
clear matrix
clear mata
set maxvar 15000

* Load in the BESIP data. Available here: https://www.britishelectionstudy.com/data-objects/panel-study-data/

use "", clear

* set working directory

cd ""


keep if wave4==1 & wave6==1

numlabel, add

save initial.dta, replace

use initial.dta, clear

**# // ANGER & OTHER PARTISAN EMOTIONS //

// Anger 

*Recode don't knows as missing: 

tab countryW6

recode labAngryW4 conAngryW4 ldAngryW4 ukipAngryW4 grnAngryW4 labAngryW6 conAngryW6 ldAngryW6 ukipAngryW6 grnAngryW6 snpAngryW4 snpAngryW6 pcAngryW4 pcAngryW6 (9999=.)

recode snpAngryW4 snpAngryW6 (0=.) (1=.) if countryW6==1

*Linking anger measures to out-parties. 
*Pre-election party id is used to determine in-parties vs out-parties:

tab partyIdW4
tab generalElectionVoteW4

gen IP_anger_W4=.
replace IP_anger_W4=conAngryW4 if generalElectionVoteW4==1
replace IP_anger_W4=labAngryW4 if generalElectionVoteW4==2
replace IP_anger_W4=ldAngryW4 if generalElectionVoteW4==3
replace IP_anger_W4=snpAngryW4 if generalElectionVoteW4==4
replace IP_anger_W4=pcAngryW4 if generalElectionVoteW4==5
replace IP_anger_W4=ukipAngryW4 if generalElectionVoteW4==6
replace IP_anger_W4=grnAngryW4 if generalElectionVoteW4==7
tab IP_anger_W4

gen IP_anger_W6=.
replace IP_anger_W6=conAngryW6 if generalElectionVoteW4==1
replace IP_anger_W6=labAngryW6 if generalElectionVoteW4==2
replace IP_anger_W6=ldAngryW6 if generalElectionVoteW4==3
replace IP_anger_W6=snpAngryW6 if generalElectionVoteW4==4
replace IP_anger_W6=pcAngryW6 if generalElectionVoteW4==5
replace IP_anger_W6=ukipAngryW6 if generalElectionVoteW4==6
replace IP_anger_W6=grnAngryW6 if generalElectionVoteW4==7
tab IP_anger_W6

drop if IP_anger_W4==.|IP_anger_W6==.

*Gen the total anger value for all parties, subtract in-party score. 

tab countryW4

tab id if countryW4!=countryW6 & (countryW4!=.&countryW6!=.)

egen anger_W4_ENG = rowtotal(conAngryW4 labAngryW4 ldAngryW4 ukipAngryW4 grnAngryW4) if countryW4==1
egen anger_W4_SCO = rowtotal(conAngryW4 labAngryW4 ldAngryW4 ukipAngryW4 grnAngryW4 snpAngryW4) if countryW4==2
egen anger_W4_WAL = rowtotal(conAngryW4 labAngryW4 ldAngryW4 ukipAngryW4 grnAngryW4 pcAngryW4) if countryW4==3

gen OP_anger_W4=.
replace OP_anger_W4=(anger_W4_ENG - IP_anger_W4)/4 if countryW4==1
replace OP_anger_W4=(anger_W4_SCO - IP_anger_W4)/5 if countryW4==2
replace OP_anger_W4=(anger_W4_WAL - IP_anger_W4)/5 if countryW4==3
tab OP_anger_W4

egen anger_W6_ENG = rowtotal(conAngryW6 labAngryW6 ldAngryW6 ukipAngryW6 grnAngryW6) if countryW4==1
egen anger_W6_SCO = rowtotal(conAngryW6 labAngryW6 ldAngryW6 ukipAngryW6 grnAngryW6 snpAngryW6) if countryW6==2
egen anger_W6_WAL = rowtotal(conAngryW6 labAngryW6 ldAngryW6 ukipAngryW6 grnAngryW6 pcAngryW6) if countryW6==3

gen OP_anger_W6=.
replace OP_anger_W6=(anger_W6_ENG - IP_anger_W6)/4 if countryW6==1
replace OP_anger_W6=(anger_W6_SCO - IP_anger_W6)/5 if countryW6==2
replace OP_anger_W6=(anger_W6_WAL - IP_anger_W6)/5 if countryW6==3

tab OP_anger_W4
tab OP_anger_W6

tab OP_anger_W4 countryW4
tab OP_anger_W6 countryW6


// Other emotions in the data 

recode labHopeW4 conHopeW4 ldHopeW4 ukipHopeW4 grnHopeW4 labHopeW6 conHopeW6 ldHopeW6 ukipHopeW6 grnHopeW6 snpHopeW4 snpHopeW6 pcHopeW4 pcHopeW6 (9999=.)

recode labFearW4 conFearW4 ldFearW4 ukipFearW4 grnFearW4 labFearW6 conFearW6 ldFearW6 ukipFearW6 grnFearW6 snpFearW4 snpFearW6 pcFearW4 pcFearW6 (9999=.)

recode labPrideW4 conPrideW4 ldPrideW4 ukipPrideW4 grnPrideW4 labPrideW6 conPrideW6 ldPrideW6 ukipPrideW6 grnPrideW6 snpPrideW4 snpPrideW6 pcPrideW4 pcPrideW6 (9999=.)

recode snpPrideW4 snpPrideW6 snpFearW4 snpFearW6 snpHopeW4 snpHopeW6 (0=.) (1=.) if countryW6==1

*Coding in-party pride, hope and fear:

gen IP_pride_W4=.
replace IP_pride_W4=conPrideW4 if generalElectionVoteW4==1
replace IP_pride_W4=labPrideW4 if generalElectionVoteW4==2
replace IP_pride_W4=ldPrideW4 if generalElectionVoteW4==3
replace IP_pride_W4=snpPrideW4 if generalElectionVoteW4==4
replace IP_pride_W4=pcPrideW4 if generalElectionVoteW4==5
replace IP_pride_W4=ukipPrideW4 if generalElectionVoteW4==6
replace IP_pride_W4=grnPrideW4 if generalElectionVoteW4==7
tab IP_pride_W4

gen IP_pride_W6=.
replace IP_pride_W6=conPrideW6 if generalElectionVoteW4==1
replace IP_pride_W6=labPrideW6 if generalElectionVoteW4==2
replace IP_pride_W6=ldPrideW6 if generalElectionVoteW4==3
replace IP_pride_W6=snpPrideW6 if generalElectionVoteW4==4
replace IP_pride_W6=pcPrideW6 if generalElectionVoteW4==5
replace IP_pride_W6=ukipPrideW6 if generalElectionVoteW4==6
replace IP_pride_W6=grnPrideW6 if generalElectionVoteW4==7
tab IP_pride_W6

gen IP_hope_W4=.
replace IP_hope_W4=conHopeW4 if generalElectionVoteW4==1
replace IP_hope_W4=labHopeW4 if generalElectionVoteW4==2
replace IP_hope_W4=ldHopeW4 if generalElectionVoteW4==3
replace IP_hope_W4=snpHopeW4 if generalElectionVoteW4==4
replace IP_hope_W4=pcHopeW4 if generalElectionVoteW4==5
replace IP_hope_W4=ukipHopeW4 if generalElectionVoteW4==6
replace IP_hope_W4=grnHopeW4 if generalElectionVoteW4==7
tab IP_hope_W4

gen IP_hope_W6=.
replace IP_hope_W6=conHopeW6 if generalElectionVoteW4==1
replace IP_hope_W6=labHopeW6 if generalElectionVoteW4==2
replace IP_hope_W6=ldHopeW6 if generalElectionVoteW4==3
replace IP_hope_W6=snpHopeW6 if generalElectionVoteW4==4
replace IP_hope_W6=pcHopeW6 if generalElectionVoteW4==5
replace IP_hope_W6=ukipHopeW6 if generalElectionVoteW4==6
replace IP_hope_W6=grnHopeW6 if generalElectionVoteW4==7
tab IP_hope_W6

gen IP_fear_W4=.
replace IP_fear_W4=conFearW4 if generalElectionVoteW4==1
replace IP_fear_W4=labFearW4 if generalElectionVoteW4==2
replace IP_fear_W4=ldFearW4 if generalElectionVoteW4==3
replace IP_fear_W4=snpFearW4 if generalElectionVoteW4==4
replace IP_fear_W4=pcFearW4 if generalElectionVoteW4==5
replace IP_fear_W4=ukipFearW4 if generalElectionVoteW4==6
replace IP_fear_W4=grnFearW4 if generalElectionVoteW4==7
tab IP_fear_W4

gen IP_fear_W6=.
replace IP_fear_W6=conFearW6 if generalElectionVoteW4==1
replace IP_fear_W6=labFearW6 if generalElectionVoteW4==2
replace IP_fear_W6=ldFearW6 if generalElectionVoteW4==3
replace IP_fear_W6=snpFearW6 if generalElectionVoteW4==4
replace IP_fear_W6=pcFearW6 if generalElectionVoteW4==5
replace IP_fear_W6=ukipFearW6 if generalElectionVoteW4==6
replace IP_fear_W6=grnFearW6 if generalElectionVoteW4==7
tab IP_fear_W6

*coding op fear:

egen fear_W4_ENG = rowtotal(conFearW4 labFearW4 ldFearW4 ukipFearW4 grnFearW4) if countryW4==1
egen fear_W4_SCO = rowtotal(conFearW4 labFearW4 ldFearW4 ukipFearW4 grnFearW4 snpFearW4) if countryW4==2
egen fear_W4_WAL = rowtotal(conFearW4 labFearW4 ldFearW4 ukipFearW4 grnFearW4 pcFearW4) if countryW4==3

tab fear_W4_ENG
tab fear_W4_SCO
tab fear_W4_WAL

gen OP_fear_W4=.
replace OP_fear_W4=(fear_W4_ENG - IP_fear_W4)/4 if countryW4==1
replace OP_fear_W4=(fear_W4_SCO - IP_fear_W4)/5 if countryW4==2
replace OP_fear_W4=(fear_W4_WAL - IP_fear_W4)/5 if countryW4==3
tab OP_fear_W4

egen fear_W6_ENG = rowtotal(conFearW6 labFearW6 ldFearW6 ukipFearW6 grnFearW6) if countryW6==1
egen fear_W6_SCO = rowtotal(conFearW6 labFearW6 ldFearW6 ukipFearW6 grnFearW6 snpFearW6) if countryW6==2
egen fear_W6_WAL = rowtotal(conFearW6 labFearW6 ldFearW6 ukipFearW6 grnFearW6 pcFearW6) if countryW6==3

gen OP_fear_W6=.
replace OP_fear_W6=(fear_W6_ENG - IP_fear_W6)/4 if countryW6==1
replace OP_fear_W6=(fear_W6_SCO - IP_fear_W6)/5 if countryW6==2
replace OP_fear_W6=(fear_W6_WAL - IP_fear_W6)/5 if countryW6==3
tab OP_fear_W6

*coding op pride

egen pride_W4_ENG = rowtotal(conPrideW4 labPrideW4 ldPrideW4 ukipPrideW4 grnPrideW4) if countryW4==1
egen pride_W4_SCO = rowtotal(conPrideW4 labPrideW4 ldPrideW4 ukipPrideW4 grnPrideW4 snpPrideW4) if countryW4==2
egen pride_W4_WAL = rowtotal(conPrideW4 labPrideW4 ldPrideW4 ukipPrideW4 grnPrideW4 pcPrideW4) if countryW4==3

tab pride_W4_ENG
tab pride_W4_SCO
tab pride_W4_WAL

gen OP_pride_W4=.
replace OP_pride_W4=(pride_W4_ENG - IP_pride_W4)/4 if countryW4==1
replace OP_pride_W4=(pride_W4_SCO - IP_pride_W4)/5 if countryW4==2
replace OP_pride_W4=(pride_W4_WAL - IP_pride_W4)/5 if countryW4==3
tab OP_pride_W4

egen pride_W6_ENG = rowtotal(conPrideW6 labPrideW6 ldPrideW6 ukipPrideW6 grnPrideW6) if countryW6==1
egen pride_W6_SCO = rowtotal(conPrideW6 labPrideW6 ldPrideW6 ukipPrideW6 grnPrideW6 snpPrideW6) if countryW6==2
egen pride_W6_WAL = rowtotal(conPrideW6 labPrideW6 ldPrideW6 ukipPrideW6 grnPrideW6 pcPrideW6) if countryW6==3

gen OP_pride_W6=.
replace OP_pride_W6=(pride_W6_ENG - IP_pride_W6)/4 if countryW6==1
replace OP_pride_W6=(pride_W6_SCO - IP_pride_W6)/5 if countryW6==2
replace OP_pride_W6=(pride_W6_WAL - IP_pride_W6)/5 if countryW6==3
tab OP_pride_W6

*coding OP hope:

egen hope_W4_ENG = rowtotal(conHopeW4 labHopeW4 ldHopeW4 ukipHopeW4 grnHopeW4) if countryW4==1
egen hope_W4_SCO = rowtotal(conHopeW4 labHopeW4 ldHopeW4 ukipHopeW4 grnHopeW4 snpHopeW4) if countryW4==2
egen hope_W4_WAL = rowtotal(conHopeW4 labHopeW4 ldHopeW4 ukipHopeW4 grnHopeW4 pcHopeW4) if countryW4==3

tab hope_W4_ENG
tab hope_W4_SCO
tab hope_W4_WAL

tab IP_hope_W4

gen OP_hope_W4=.
replace OP_hope_W4=(hope_W4_ENG - IP_hope_W4)/4 if countryW4==1
replace OP_hope_W4=(hope_W4_SCO - IP_hope_W4)/5 if countryW4==2
replace OP_hope_W4=(hope_W4_WAL - IP_hope_W4)/5 if countryW4==3
tab OP_hope_W4

egen hope_W6_ENG = rowtotal(conHopeW6 labHopeW6 ldHopeW6 ukipHopeW6 grnHopeW6) if countryW6==1
egen hope_W6_SCO = rowtotal(conHopeW6 labHopeW6 ldHopeW6 ukipHopeW6 grnHopeW6 snpHopeW6) if countryW6==2
egen hope_W6_WAL = rowtotal(conHopeW6 labHopeW6 ldHopeW6 ukipHopeW6 grnHopeW6 pcHopeW6) if countryW6==3

tab hope_W6_ENG
tab hope_W6_SCO
tab hope_W6_WAL

tab IP_hope_W6

gen OP_hope_W6=.
replace OP_hope_W6=(hope_W6_ENG - IP_hope_W6)/4 if countryW6==1
replace OP_hope_W6=(hope_W6_SCO - IP_hope_W6)/5 if countryW6==2
replace OP_hope_W6=(hope_W6_WAL - IP_hope_W6)/5 if countryW6==3
tab OP_hope_W6

recode OP_hope_W6 (-.2=.)

**# // AFFECTIVE POLARISATION //

recode likeConW6 likeConW4 likeLabW6 likeLabW4 likeLDW6 likeLDW4 likeUKIPW6 likeUKIPW4 likeGrnW6 likeGrnW4 likeSNPW4 likeSNPW6 likePCW4 likePCW6 (9999=.)

tab countryW6


*Individual mean like-dislike - ENGLAND:
gen mean_ldl_6_ENG = ((0.368*likeConW6) + (0.304*likeLabW6) + (0.079*likeLDW6) + (0.126*likeUKIPW6) + (0.038*likeGrnW6)) if ((likeConW6!=. & likeLabW6!=. & likeLDW6!=. & likeUKIPW6!=. & likeGrnW6!=.) & countryW6==1)

gen mean_ldl_4_ENG = ((0.368*likeConW4) + (0.304*likeLabW4) + (0.079*likeLDW4) + (0.126*likeUKIPW4) + (0.038*likeGrnW4)) if ((likeConW4!=. & likeLabW4!=. & likeLDW4!=. & likeUKIPW4!=. & likeGrnW4!=.) & countryW4==1)

summ mean_ldl_6_ENG
summ mean_ldl_4_ENG

*SCOTLAND:

gen mean_ldl_6_SCO = ((0.368*likeConW6) + (0.304*likeLabW6) + (0.079*likeLDW6) + (0.126*likeUKIPW6) + (0.038*likeGrnW6) + (0.047*likeSNPW6)) if ((likeConW6!=. & likeLabW6!=. & likeLDW6!=. & likeUKIPW6!=. & likeGrnW6!=. & likeSNPW6!=.) & countryW6==2)

gen mean_ldl_4_SCO = ((0.368*likeConW4) + (0.304*likeLabW4) + (0.079*likeLDW4) + (0.126*likeUKIPW4) + (0.038*likeGrnW4) + (0.047*likeSNPW4)) if ((likeConW4!=. & likeLabW4!=. & likeLDW4!=. & likeUKIPW4!=. & likeGrnW4!=. & likeSNPW4!=.) & countryW4==2)

summ mean_ldl_6_SCO
summ mean_ldl_4_SCO

*WALES:

gen mean_ldl_6_WAL = ((0.368*likeConW6) + (0.304*likeLabW6) + (0.079*likeLDW6) + (0.126*likeUKIPW6) + (0.038*likeGrnW6) + (0.006*likePCW6)) if ((likeConW6!=. & likeLabW6!=. & likeLDW6!=. & likeUKIPW6!=. & likeGrnW6!=. & likePCW6!=.) & countryW6==3)

gen mean_ldl_4_WAL = ((0.368*likeConW4) + (0.304*likeLabW4) + (0.079*likeLDW4) + (0.126*likeUKIPW4) + (0.038*likeGrnW4) + (0.006*likePCW4)) if ((likeConW4!=. & likeLabW4!=. & likeLDW4!=. & likeUKIPW4!=. & likeGrnW4!=. & likePCW4!=.) & countryW4==3)

summ mean_ldl_6_WAL
summ mean_ldl_4_WAL


*AP scores - ENGLAND: 

gen affpol_W6_ENG = sqrt((0.368*((likeConW6 - mean_ldl_6_ENG)^2)) + (0.304*((likeLabW6 - mean_ldl_6_ENG)^2)) + (0.079*((likeLDW6 - mean_ldl_6_ENG)^2)) + (0.126*((likeUKIPW6 - mean_ldl_6_ENG)^2)) + (0.038*((likeGrnW6 - mean_ldl_6_ENG)^2))) if countryW6==1


gen affpol_W4_ENG = sqrt((0.368*((likeConW4 - mean_ldl_4_ENG)^2)) + (0.304*((likeLabW4 - mean_ldl_4_ENG)^2)) + (0.079*((likeLDW4 - mean_ldl_4_ENG)^2)) + (0.126*((likeUKIPW4 - mean_ldl_4_ENG)^2)) + (0.038*((likeGrnW4 - mean_ldl_4_ENG)^2))) if countryW4==1

summ affpol_W4_ENG
summ affpol_W6_ENG


*SCOTLAND:

gen affpol_W6_SCO = sqrt((0.368*((likeConW6 - mean_ldl_6_SCO)^2)) + (0.304*((likeLabW6 - mean_ldl_6_SCO)^2)) + (0.079*((likeLDW6 - mean_ldl_6_SCO)^2)) + (0.126*((likeUKIPW6 - mean_ldl_6_SCO)^2)) + (0.038*((likeGrnW6 - mean_ldl_6_SCO)^2)) + (0.047*((likeSNPW6 - mean_ldl_6_SCO)^2))) if countryW6==2

gen affpol_W4_SCO = sqrt((0.368*((likeConW4 - mean_ldl_4_SCO)^2)) + (0.304*((likeLabW4 - mean_ldl_4_SCO)^2)) + (0.079*((likeLDW4 - mean_ldl_4_SCO)^2)) + (0.126*((likeUKIPW4 - mean_ldl_4_SCO)^2)) + (0.038*((likeGrnW4 - mean_ldl_4_SCO)^2)) + (0.047*((likeSNPW4 - mean_ldl_4_SCO)^2))) if countryW4==2

summ affpol_W4_SCO
summ affpol_W6_SCO

*WALES:

gen affpol_W6_WAL = sqrt((0.368*((likeConW6 - mean_ldl_6_WAL)^2)) + (0.304*((likeLabW6 - mean_ldl_6_WAL)^2)) + (0.079*((likeLDW6 - mean_ldl_6_WAL)^2)) + (0.126*((likeUKIPW6 - mean_ldl_6_WAL)^2)) + (0.038*((likeGrnW6 - mean_ldl_6_WAL)^2)) + (0.006*((likePCW6 - mean_ldl_6_WAL)^2))) if countryW6==3

gen affpol_W4_WAL = sqrt((0.368*((likeConW4 - mean_ldl_4_WAL)^2)) + (0.304*((likeLabW4 - mean_ldl_4_WAL)^2)) + (0.079*((likeLDW4 - mean_ldl_4_WAL)^2)) + (0.126*((likeUKIPW4 - mean_ldl_4_WAL)^2)) + (0.038*((likeGrnW4 - mean_ldl_4_WAL)^2)) + (0.006*((likePCW4 - mean_ldl_4_WAL)^2))) if countryW4==3

summ affpol_W4_WAL
summ affpol_W6_WAL

tab countryW6 if affpol_W4_WAL==0
tab countryW6 if affpol_W6_WAL==0

*Now combine:

gen affpol_W6=.
replace affpol_W6=affpol_W6_ENG if countryW6==1
replace affpol_W6=affpol_W6_SCO if countryW6==2
replace affpol_W6=affpol_W6_WAL if countryW6==3

gen affpol_W4=.
replace affpol_W4=affpol_W4_ENG if countryW4==1
replace affpol_W4=affpol_W4_SCO if countryW4==2
replace affpol_W4=affpol_W4_WAL if countryW4==3

summ affpol_W6 if countryW6==3
summ affpol_W4 if countryW6==2

summ affpol_W6
summ affpol_W4

/// In-party like ///

*Using pre-election party ID:

tab generalElectionVoteW4

gen IP_like_W4=.
replace IP_like_W4=likeConW4 if generalElectionVoteW4==1
replace IP_like_W4=likeLabW4 if generalElectionVoteW4==2
replace IP_like_W4=likeLDW4 if generalElectionVoteW4==3
replace IP_like_W4=likeSNPW4 if generalElectionVoteW4==4
replace IP_like_W4=likePCW4 if generalElectionVoteW4==5
replace IP_like_W4=likeUKIPW4 if generalElectionVoteW4==6
replace IP_like_W4=likeGrnW4 if generalElectionVoteW4==7
tab IP_like_W4

 gen IP_like_W6=.
replace IP_like_W6=likeConW6 if generalElectionVoteW4==1
replace IP_like_W6=likeLabW6 if generalElectionVoteW4==2
replace IP_like_W6=likeLDW6 if generalElectionVoteW4==3
replace IP_like_W6=likeSNPW6 if generalElectionVoteW4==4
replace IP_like_W6=likePCW6 if generalElectionVoteW4==5
replace IP_like_W6=likeUKIPW6 if generalElectionVoteW4==6
replace IP_like_W6=likeGrnW6 if generalElectionVoteW4==7
tab IP_like_W6

/// Out-party dislike - single party ///

gen OP_dislike_LLP_W4=.

*ENGLAND: 
replace OP_dislike_LLP_W4=likeConW4 if ((likeConW4<=likeLabW4) & (likeConW4<=likeLDW4) & (likeConW4<=likeUKIPW4) & (likeConW4<=likeGrnW4)) & countryW4==1
replace OP_dislike_LLP_W4=likeLabW4 if ((likeLabW4<=likeConW4) & (likeLabW4<=likeLDW4) & (likeLabW4<=likeUKIPW4) & (likeLabW4<=likeGrnW4)) & countryW4==1
replace OP_dislike_LLP_W4=likeLDW4 if ((likeLDW4<=likeConW4) & (likeLDW4<=likeLabW4) & (likeLDW4<=likeUKIPW4)  & (likeLDW4<=likeGrnW4)) & countryW4==1
replace OP_dislike_LLP_W4=likeUKIPW4 if ((likeUKIPW4<=likeConW4) & (likeUKIPW4<=likeLabW4) & (likeUKIPW4<=likeLDW4) & (likeUKIPW4<=likeGrnW4)) & countryW4==1
replace OP_dislike_LLP_W4=likeGrnW4 if ((likeGrnW4<=likeConW4) & (likeGrnW4<=likeLabW4) & (likeGrnW4<=likeLDW4) & (likeGrnW4<=likeUKIPW4)) & countryW4==1

*SCOTLAND: 

replace OP_dislike_LLP_W4=likeConW4 if ((likeConW4<=likeLabW4) & (likeConW4<=likeLDW4) & (likeConW4<=likeUKIPW4) & (likeConW4<=likeGrnW4) & (likeConW4<=likeSNPW4)) & countryW4==2
replace OP_dislike_LLP_W4=likeLabW4 if ((likeLabW4<=likeConW4) & (likeLabW4<=likeLDW4) & (likeLabW4<=likeUKIPW4) & (likeLabW4<=likeGrnW4) & (likeLabW4<=likeSNPW4)) & countryW4==2
replace OP_dislike_LLP_W4=likeLDW4 if ((likeLDW4<=likeConW4) & (likeLDW4<=likeLabW4) & (likeLDW4<=likeUKIPW4)  & (likeLDW4<=likeGrnW4) & (likeLDW4<=likeSNPW4)) & countryW4==2
replace OP_dislike_LLP_W4=likeUKIPW4 if ((likeUKIPW4<=likeConW4) & (likeUKIPW4<=likeLabW4) & (likeUKIPW4<=likeLDW4) & (likeUKIPW4<=likeGrnW4) & (likeUKIPW4<=likeSNPW4)) & countryW4==2
replace OP_dislike_LLP_W4=likeGrnW4 if ((likeGrnW4<=likeConW4) & (likeGrnW4<=likeLabW4) & (likeGrnW4<=likeLDW4) & (likeGrnW4<=likeUKIPW4) & (likeGrnW4<=likeSNPW4)) & countryW4==2
replace OP_dislike_LLP_W4=likeSNPW4 if ((likeSNPW4<=likeConW4) & (likeSNPW4<=likeLabW4) & (likeSNPW4<=likeLDW4) & (likeSNPW4<=likeUKIPW4) & (likeSNPW4<=likeGrnW4)) & countryW4==2

*WALES:

replace OP_dislike_LLP_W4=likeConW4 if ((likeConW4<=likeLabW4) & (likeConW4<=likeLDW4) & (likeConW4<=likeUKIPW4) & (likeConW4<=likeGrnW4) & (likeConW4<=likePCW4)) & countryW4==3
replace OP_dislike_LLP_W4=likeLabW4 if ((likeLabW4<=likeConW4) & (likeLabW4<=likeLDW4) & (likeLabW4<=likeUKIPW4) & (likeLabW4<=likeGrnW4) & (likeLabW4<=likePCW4)) & countryW4==3
replace OP_dislike_LLP_W4=likeLDW4 if ((likeLDW4<=likeConW4) & (likeLDW4<=likeLabW4) & (likeLDW4<=likeUKIPW4)  & (likeLDW4<=likeGrnW4) & (likeLDW4<=likePCW4)) & countryW4==3
replace OP_dislike_LLP_W4=likeUKIPW4 if ((likeUKIPW4<=likeConW4) & (likeUKIPW4<=likeLabW4) & (likeUKIPW4<=likeLDW4) & (likeUKIPW4<=likeGrnW4) & (likeUKIPW4<=likePCW4)) & countryW4==3
replace OP_dislike_LLP_W4=likeGrnW4 if ((likeGrnW4<=likeConW4) & (likeGrnW4<=likeLabW4) & (likeGrnW4<=likeLDW4) & (likeGrnW4<=likeUKIPW4) & (likeGrnW4<=likePCW4)) & countryW4==3
replace OP_dislike_LLP_W4=likePCW4 if ((likePCW4<=likeConW4) & (likePCW4<=likeLabW4) & (likePCW4<=likeLDW4) & (likePCW4<=likeUKIPW4) & (likePCW4<=likeGrnW4)) & countryW4==3


tab OP_dislike_LLP_W4

*Wave 6:
gen OP_dislike_LLP_W6=.

*ENGLAND: 

replace OP_dislike_LLP_W6=likeConW6 if ((likeConW6<=likeLabW6) & (likeConW6<=likeLDW6) & (likeConW6<=likeUKIPW6) & (likeConW6<=likeGrnW6)) & countryW6==1
replace OP_dislike_LLP_W6=likeLabW6 if ((likeLabW6<=likeConW6) & (likeLabW6<=likeLDW6) & (likeLabW6<=likeUKIPW6) & (likeLabW6<=likeGrnW6)) & countryW6==1
replace OP_dislike_LLP_W6=likeLDW6 if ((likeLDW6<=likeConW6) & (likeLDW6<=likeLabW6) & (likeLDW6<=likeUKIPW6)  & (likeLDW6<=likeGrnW6)) & countryW6==1
replace OP_dislike_LLP_W6=likeUKIPW6 if ((likeUKIPW6<=likeConW6) & (likeUKIPW6<=likeLabW6) & (likeUKIPW6<=likeLDW6) & (likeUKIPW6<=likeGrnW6)) & countryW6==1
replace OP_dislike_LLP_W6=likeGrnW6 if ((likeGrnW6<=likeConW6) & (likeGrnW6<=likeLabW6) & (likeGrnW6<=likeLDW6) & (likeGrnW6<=likeUKIPW6)) & countryW6==1

*SCOTLAND: 

replace OP_dislike_LLP_W6=likeConW6 if ((likeConW6<=likeLabW6) & (likeConW6<=likeLDW6) & (likeConW6<=likeUKIPW6) & (likeConW6<=likeGrnW6) & (likeConW6<=likeSNPW6)) & countryW6==2
replace OP_dislike_LLP_W6=likeLabW6 if ((likeLabW6<=likeConW6) & (likeLabW6<=likeLDW6) & (likeLabW6<=likeUKIPW6) & (likeLabW6<=likeGrnW6) & (likeLabW6<=likeSNPW6)) & countryW6==2
replace OP_dislike_LLP_W6=likeLDW6 if ((likeLDW6<=likeConW6) & (likeLDW6<=likeLabW6) & (likeLDW6<=likeUKIPW6)  & (likeLDW6<=likeGrnW6) & (likeLDW6<=likeSNPW6)) & countryW6==2
replace OP_dislike_LLP_W6=likeUKIPW6 if ((likeUKIPW6<=likeConW6) & (likeUKIPW6<=likeLabW6) & (likeUKIPW6<=likeLDW6) & (likeUKIPW6<=likeGrnW6) & (likeUKIPW6<=likeSNPW6)) & countryW6==2
replace OP_dislike_LLP_W6=likeGrnW6 if ((likeGrnW6<=likeConW6) & (likeGrnW6<=likeLabW6) & (likeGrnW6<=likeLDW6) & (likeGrnW6<=likeUKIPW6) & (likeGrnW6<=likeSNPW6)) & countryW6==2
replace OP_dislike_LLP_W6=likeSNPW6 if ((likeSNPW6<=likeConW6) & (likeSNPW6<=likeLabW6) & (likeSNPW6<=likeLDW6) & (likeSNPW6<=likeUKIPW6) & (likeSNPW6<=likeGrnW6)) & countryW6==2

*WALES:

replace OP_dislike_LLP_W6=likeConW6 if ((likeConW6<=likeLabW6) & (likeConW6<=likeLDW6) & (likeConW6<=likeUKIPW6) & (likeConW6<=likeGrnW6) & (likeConW6<=likePCW6)) & countryW6==3
replace OP_dislike_LLP_W6=likeLabW6 if ((likeLabW6<=likeConW6) & (likeLabW6<=likeLDW6) & (likeLabW6<=likeUKIPW6) & (likeLabW6<=likeGrnW6) & (likeLabW6<=likePCW6)) & countryW6==3
replace OP_dislike_LLP_W6=likeLDW6 if ((likeLDW6<=likeConW6) & (likeLDW6<=likeLabW6) & (likeLDW6<=likeUKIPW6)  & (likeLDW6<=likeGrnW6) & (likeLDW6<=likePCW6)) & countryW6==3
replace OP_dislike_LLP_W6=likeUKIPW6 if ((likeUKIPW6<=likeConW6) & (likeUKIPW6<=likeLabW6) & (likeUKIPW6<=likeLDW6) & (likeUKIPW6<=likeGrnW6) & (likeUKIPW6<=likePCW6)) & countryW6==3
replace OP_dislike_LLP_W6=likeGrnW6 if ((likeGrnW6<=likeConW6) & (likeGrnW6<=likeLabW6) & (likeGrnW6<=likeLDW6) & (likeGrnW6<=likeUKIPW6) & (likeGrnW6<=likePCW6)) & countryW6==3
replace OP_dislike_LLP_W6=likePCW6 if ((likePCW6<=likeConW6) & (likePCW6<=likeLabW6) & (likePCW6<=likeLDW6) & (likePCW6<=likeUKIPW6) & (likePCW6<=likeGrnW6)) & countryW6==3

tab OP_dislike_LLP_W6

gen OP_dislike_LLP_R_W4=(0-OP_dislike_LLP_W4)+10
summ OP_dislike_LLP_R_W4
gen OP_dislike_LLP_R_W6=(0-OP_dislike_LLP_W6)+10
summ OP_dislike_LLP_R_W6

/// Out-party dislike - multi-party ///

*Based on pre-election party id. So this is the average (mean) like score for all parties other than the one the in-party, weighted by vote share:

*ENGLAND:
gen OP_dislike_MPV_W4=.
replace OP_dislike_MPV_W4=((0.304*likeLabW4) + (0.079*likeLDW4) + (0.126*likeUKIPW4) + (0.038*likeGrnW4)/4) if generalElectionVoteW4==1 & countryW4==1
replace OP_dislike_MPV_W4=((0.368*likeConW4) + (0.079*likeLDW4) + (0.126*likeUKIPW4) + (0.038*likeGrnW4)/4) if generalElectionVoteW4==2 & countryW4==1
replace OP_dislike_MPV_W4=((0.368*likeConW4) + (0.304*likeLabW4) + (0.126*likeUKIPW4) + (0.038*likeGrnW4)/4) if generalElectionVoteW4==3 & countryW4==1
replace OP_dislike_MPV_W4=((0.368*likeConW4) + (0.304*likeLabW4) + (0.079*likeLDW4) + (0.038*likeGrnW4)/4) if generalElectionVoteW4==6 & countryW4==1
replace OP_dislike_MPV_W4=((0.368*likeConW4) + (0.304*likeLabW4) + (0.079*likeLDW4) + (0.126*likeUKIPW4)/4) if generalElectionVoteW4==7 & countryW4==1
summ OP_dislike_MPV_W4

gen OP_dislike_MPV_W6=.
replace OP_dislike_MPV_W6=((0.304*likeLabW6) + (0.079*likeLDW6) + (0.126*likeUKIPW6) + (0.038*likeGrnW6)/4) if generalElectionVoteW4==1 & countryW6==1
replace OP_dislike_MPV_W6=((0.368*likeConW6) + (0.079*likeLDW6) + (0.126*likeUKIPW6) + (0.038*likeGrnW6)/4) if generalElectionVoteW4==2 & countryW6==1
replace OP_dislike_MPV_W6=((0.368*likeConW6) + (0.304*likeLabW6) + (0.126*likeUKIPW6) + (0.038*likeGrnW6)/4) if generalElectionVoteW4==3 & countryW6==1
replace OP_dislike_MPV_W6=((0.368*likeConW6) + (0.304*likeLabW6) + (0.079*likeLDW6) + (0.038*likeGrnW6)/4) if generalElectionVoteW4==6 & countryW6==1
replace OP_dislike_MPV_W6=((0.368*likeConW6) + (0.304*likeLabW6) + (0.079*likeLDW6) + (0.126*likeUKIPW6)/4) if generalElectionVoteW4==7 & countryW6==1
summ OP_dislike_MPV_W6

*SCOTLAND: 
replace OP_dislike_MPV_W4=((0.304*likeLabW4) + (0.079*likeLDW4) + (0.126*likeUKIPW4) + (0.038*likeGrnW4) + (0.047*likeSNPW4)/5) if generalElectionVoteW4==1 & countryW4==2
replace OP_dislike_MPV_W4=((0.368*likeConW4) + (0.079*likeLDW4) + (0.126*likeUKIPW4) + (0.038*likeGrnW4) + (0.047*likeSNPW4)/4) if generalElectionVoteW4==2 & countryW4==2
replace OP_dislike_MPV_W4=((0.368*likeConW4) + (0.304*likeLabW4) + (0.126*likeUKIPW4) + (0.038*likeGrnW4)+ (0.047*likeSNPW4)/4) if generalElectionVoteW4==3 & countryW4==2
replace OP_dislike_MPV_W4=((0.368*likeConW4) + (0.304*likeLabW4) + (0.126*likeUKIPW4) + (0.038*likeGrnW4)+ (0.079*likeLDW4)/4) if generalElectionVoteW4==4 & countryW4==2
replace OP_dislike_MPV_W4=((0.368*likeConW4) + (0.304*likeLabW4) + (0.126*likeLDW4) + (0.038*likeGrnW4)+ (0.047*likeSNPW4)/4) if generalElectionVoteW4==6 & countryW4==2
replace OP_dislike_MPV_W4=((0.368*likeConW4) + (0.304*likeLabW4) + (0.126*likeUKIPW4) + (0.038*likeLDW4)+ (0.047*likeSNPW4)/4) if generalElectionVoteW4==7 & countryW4==2

replace OP_dislike_MPV_W6=((0.304*likeLabW6) + (0.079*likeLDW6) + (0.126*likeUKIPW6) + (0.038*likeGrnW6) + (0.047*likeSNPW6)/5) if generalElectionVoteW4==1 & countryW6==2
replace OP_dislike_MPV_W6=((0.368*likeConW6) + (0.079*likeLDW6) + (0.126*likeUKIPW6) + (0.038*likeGrnW6) + (0.047*likeSNPW6)/4) if generalElectionVoteW4==2 & countryW6==2
replace OP_dislike_MPV_W6=((0.368*likeConW6) + (0.304*likeLabW6) + (0.126*likeUKIPW6) + (0.038*likeGrnW6)+ (0.047*likeSNPW6)/4) if generalElectionVoteW4==3 & countryW6==2
replace OP_dislike_MPV_W6=((0.368*likeConW6) + (0.304*likeLabW6) + (0.126*likeUKIPW6) + (0.038*likeGrnW6)+ (0.079*likeLDW6)/4) if generalElectionVoteW4==4 & countryW6==2
replace OP_dislike_MPV_W6=((0.368*likeConW6) + (0.304*likeLabW6) + (0.126*likeLDW6) + (0.038*likeGrnW6)+ (0.047*likeSNPW6)/4) if generalElectionVoteW4==6 & countryW6==2
replace OP_dislike_MPV_W6=((0.368*likeConW6) + (0.304*likeLabW6) + (0.126*likeUKIPW6) + (0.038*likeLDW6)+ (0.047*likeSNPW6)/4) if generalElectionVoteW4==7 & countryW6==2

*WALES:
replace OP_dislike_MPV_W4=((0.304*likeLabW4) + (0.079*likeLDW4) + (0.126*likeUKIPW4) + (0.038*likeGrnW4) + (0.006*likePCW4)/5) if generalElectionVoteW4==1 & countryW4==3
replace OP_dislike_MPV_W4=((0.368*likeConW4) + (0.079*likeLDW4) + (0.126*likeUKIPW4) + (0.038*likeGrnW4) + (0.006*likePCW4)/4) if generalElectionVoteW4==2 & countryW4==3
replace OP_dislike_MPV_W4=((0.368*likeConW4) + (0.304*likeLabW4) + (0.126*likeUKIPW4) + (0.038*likeGrnW4)+ (0.006*likePCW4)/4) if generalElectionVoteW4==3 & countryW4==3
replace OP_dislike_MPV_W4=((0.368*likeConW4) + (0.304*likeLabW4) + (0.126*likeUKIPW4) + (0.038*likeGrnW4)+ (0.079*likeLDW4)/4) if generalElectionVoteW4==5 & countryW4==3
replace OP_dislike_MPV_W4=((0.368*likeConW4) + (0.304*likeLabW4) + (0.126*likeLDW4) + (0.038*likeGrnW4)+ (0.006*likePCW4)/4) if generalElectionVoteW4==6 & countryW4==3
replace OP_dislike_MPV_W4=((0.368*likeConW4) + (0.304*likeLabW4) + (0.126*likeUKIPW4) + (0.038*likeLDW4)+ (0.006*likePCW4)/4) if generalElectionVoteW4==7 & countryW4==3

replace OP_dislike_MPV_W6=((0.304*likeLabW6) + (0.079*likeLDW6) + (0.126*likeUKIPW6) + (0.038*likeGrnW6) + (0.006*likePCW6)/5) if generalElectionVoteW4==1 & countryW6==3
replace OP_dislike_MPV_W6=((0.368*likeConW6) + (0.079*likeLDW6) + (0.126*likeUKIPW6) + (0.038*likeGrnW6) + (0.006*likePCW6)/4) if generalElectionVoteW4==2 & countryW6==3
replace OP_dislike_MPV_W6=((0.368*likeConW6) + (0.304*likeLabW6) + (0.126*likeUKIPW6) + (0.038*likeGrnW6)+ (0.006*likePCW6)/4) if generalElectionVoteW4==3 & countryW6==3
replace OP_dislike_MPV_W6=((0.368*likeConW6) + (0.304*likeLabW6) + (0.126*likeUKIPW6) + (0.038*likeGrnW6)+ (0.079*likeLDW6)/4) if generalElectionVoteW4==5 & countryW6==3
replace OP_dislike_MPV_W6=((0.368*likeConW6) + (0.304*likeLabW6) + (0.126*likeLDW6) + (0.038*likeGrnW6)+ (0.006*likePCW6)/4) if generalElectionVoteW4==6 & countryW6==3
replace OP_dislike_MPV_W6=((0.368*likeConW6) + (0.304*likeLabW6) + (0.126*likeUKIPW6) + (0.038*likeLDW6)+ (0.006*likePCW6)/4) if generalElectionVoteW4==7 & countryW6==3

summ OP_dislike_MPV_W4
summ OP_dislike_MPV_W6

*Reverse coding:
gen OP_dislike_MPV_R_W4=(0-OP_dislike_MPV_W4)+10
summ OP_dislike_MPV_R_W4
gen OP_dislike_MPV_R_W6=(0-OP_dislike_MPV_W6)+10
summ OP_dislike_MPV_R_W6

**# // OTHER POTENTIAL MECHANISMS //

*Perceived ideological polarization, weighted by 2015 VS. Follow the AP code, but swap out like-dislike for party lr perceptions:

recode lrConW6 lrConW4 lrLabW6 lrLabW4 lrLDW6 lrLDW4 lrUKIPW6 lrUKIPW4 lrgreensW6 lrgreensW4 lrSNPW4 lrSNPW6 lrPCW4 lrPCW6 (9999=.)

gen mean_lr_6 =((0.368*lrConW6) + (0.304*lrLabW6) + (0.079*lrLDW6) + (0.126*lrUKIPW6) + (0.038*lrgreensW6)) if ((likeConW6!=. & likeLabW6!=. & likeLDW6!=. & likeUKIPW6!=. & likeGrnW6!=.) & countryW6==1)

replace mean_lr_6 =((0.368*lrConW6) + (0.304*lrLabW6) + (0.079*lrLDW6) + (0.126*lrUKIPW6) + (0.038*lrgreensW6) + (0.047*lrSNPW6)) if ((likeConW6!=. & likeLabW6!=. & likeLDW6!=. & likeUKIPW6!=. & likeGrnW6!=. & likeSNPW6!=.) & countryW6==2)

replace mean_lr_6 =((0.368*lrConW6) + (0.304*lrLabW6) + (0.079*lrLDW6) + (0.126*lrUKIPW6) + (0.038*lrgreensW6) + (0.006*lrPCW6)) if ((likeConW6!=. & likeLabW6!=. & likeLDW6!=. & likeUKIPW6!=. & likeGrnW6!=. & likePCW6!=.) & countryW6==3)

gen mean_lr_4 =((0.368*lrConW4) + (0.304*lrLabW4) + (0.079*lrLDW4) + (0.126*lrUKIPW4) + (0.038*lrgreensW4)) if ((likeConW4!=. & likeLabW4!=. & likeLDW4!=. & likeUKIPW4!=. & likeGrnW4!=.) & countryW4==1)

replace mean_lr_4 =((0.368*lrConW4) + (0.304*lrLabW4) + (0.079*lrLDW4) + (0.126*lrUKIPW4) + (0.038*lrgreensW4) + (0.047*lrSNPW4)) if ((likeConW4!=. & likeLabW4!=. & likeLDW4!=. & likeUKIPW4!=. & likeGrnW4!=. & likeSNPW4!=.) & countryW4==2)

replace mean_lr_4 =((0.368*lrConW4) + (0.304*lrLabW4) + (0.079*lrLDW4) + (0.126*lrUKIPW4) + (0.038*lrgreensW4) + (0.006*lrPCW4)) if ((likeConW4!=. & likeLabW4!=. & likeLDW4!=. & likeUKIPW4!=. & likeGrnW4!=. & likePCW4!=.) & countryW4==3)

summ mean_lr_4
summ mean_lr_6


gen ideopol_W6 = sqrt((0.368*((lrConW6 - mean_lr_6)^2)) + (0.304*((lrLabW6 - mean_lr_6)^2)) + (0.079*((lrLDW6 - mean_lr_6)^2)) + (0.126*((lrUKIPW6 - mean_lr_6)^2)) + (0.038*((lrgreensW6 - mean_lr_6)^2))) if countryW6==1

replace ideopol_W6 = sqrt((0.368*((lrConW6 - mean_lr_6)^2)) + (0.304*((lrLabW6 - mean_lr_6)^2)) + (0.079*((lrLDW6 - mean_lr_6)^2)) + (0.126*((lrUKIPW6 - mean_lr_6)^2)) + (0.038*((lrgreensW6 - mean_lr_6)^2)) + (0.047*((lrSNPW6 - mean_lr_6)^2))) if countryW6==2

replace ideopol_W6 = sqrt((0.368*((lrConW6 - mean_lr_6)^2)) + (0.304*((lrLabW6 - mean_lr_6)^2)) + (0.079*((lrLDW6 - mean_lr_6)^2)) + (0.126*((lrUKIPW6 - mean_lr_6)^2)) + (0.038*((lrgreensW6 - mean_lr_6)^2)) + (0.006*((lrPCW6 - mean_lr_6)^2))) if countryW6==3

gen ideopol_W4 = sqrt((0.368*((lrConW4 - mean_lr_4)^2)) + (0.304*((lrLabW4 - mean_lr_4)^2)) + (0.079*((lrLDW4 - mean_lr_4)^2)) + (0.126*((lrUKIPW4 - mean_lr_4)^2)) + (0.038*((lrgreensW4 - mean_lr_4)^2))) if countryW4==1

replace ideopol_W4 = sqrt((0.368*((lrConW4 - mean_lr_4)^2)) + (0.304*((lrLabW4 - mean_lr_4)^2)) + (0.079*((lrLDW4 - mean_lr_4)^2)) + (0.126*((lrUKIPW4 - mean_lr_4)^2)) + (0.038*((lrgreensW4 - mean_lr_4)^2)) + (0.047*((lrSNPW4 - mean_lr_4)^2))) if countryW4==2

replace ideopol_W4 = sqrt((0.368*((lrConW4 - mean_lr_4)^2)) + (0.304*((lrLabW4 - mean_lr_4)^2)) + (0.079*((lrLDW4 - mean_lr_4)^2)) + (0.126*((lrUKIPW4 - mean_lr_4)^2)) + (0.038*((lrgreensW4 - mean_lr_4)^2)) + (0.006*((lrPCW4 - mean_lr_4)^2))) if countryW4==3

summ ideopol_W4
summ ideopol_W6

*Strength of party attachment: 

tab partyIdStrengthW4
tab partyIdStrengthW6

recode partyIdStrengthW4 (1=3) (2=2) (3=1) (9999=.), gen(pidstrengthW4)
recode partyIdStrengthW6 (1=3) (2=2) (3=1) (9999=.), gen(pidstrengthW6)

*Political interest/engagement:

recode electionInterestW4 electionInterestW6 (9999=.)
tab electionInterestW4
tab electionInterestW6

*Political information consumption:

recode infoSourceTVW4 infoSourcePaperW4 infoSourceRadioW4 infoSourceInternetW4 infoSourceTVW6 infoSourcePaperW6 infoSourceRadioW6 infoSourceInternetW6 (1=0) (2=1) (3=2) (4=3) (5=4) (9999=.)

tab infoSourceTVW4

gen TVinfoW4=infoSourceTVW4/4
gen PaperinfoW4=infoSourcePaperW4/4
gen RadioinfoW4=infoSourceRadioW4/4
gen InternetinfoW4=infoSourceInternetW4/4

gen TVinfoW6=infoSourceTVW6/4
gen PaperinfoW6=infoSourcePaperW6/4
gen RadioinfoW6=infoSourceRadioW6/4
gen InternetinfoW6=infoSourceInternetW6/4

*Coding media consumption measures at wave 4: 

egen neutralW4=rowmean(TVinfoW4 RadioinfoW4)
egen partisanW4=rowmean(PaperinfoW4 InternetinfoW4)

*Coding media consumption measures at wave 6: 
egen neutralW6=rowmean(TVinfoW6 RadioinfoW6)
egen partisanW6=rowmean(PaperinfoW6 InternetinfoW6)



*media consumption*interest

gen neutral_mediaW4=neutralW4*electionInterestW4
gen partisan_mediaW4=partisanW4*electionInterestW4

gen neutral_mediaW6=neutralW6*electionInterestW6
gen partisan_mediaW6=partisanW6*electionInterestW6

summ neutral_mediaW4
summ partisan_mediaW6

**# MODERATORS AND CONTROLS 

// Potential moderators

*Win/loss:
 
tab generalElectionVoteW6
recode generalElectionVoteW6 (1=1) (2/9=0) (9999=.), gen(wingovW6)
tab wingovW6
gen wingovW4=0
tab wingovW4
replace wingovW4=. if wingovW6==.

save initial.dta, replace

* Load in the BES constituency election results file. Available here: https://www.britishelectionstudy.com/data-objects/linked-data/

use "", clear

keep Winner15 pano

rename pano panoW6

* save as new .dta file

save 2015_winners.dta, replace

* re-open BES panel file

use initial.dta, clear

* merge with constituency info

merge m:1 panoW6 using 2015_winners.dta

tab Winner15

tab generalElectionVoteW6

*coding constituency outcome variable

gen winconW4=0

gen winconW6=1 if (Winner15==1&generalElectionVoteW6==1)|(Winner15==2&generalElectionVoteW6==2)|(Winner15==3&generalElectionVoteW6==3)|(Winner15==4&generalElectionVoteW6==4)|(Winner15==5&generalElectionVoteW6==5)|(Winner15==6&generalElectionVoteW6==6)|(Winner15==7&generalElectionVoteW6==7)
replace winconW6=0 if (Winner15==1&generalElectionVoteW6!=1)|(Winner15==2&generalElectionVoteW6!=2)|(Winner15==3&generalElectionVoteW6!=3)|(Winner15==4&generalElectionVoteW6!=4)|(Winner15==5&generalElectionVoteW6!=5)|(Winner15==6&generalElectionVoteW6!=6)|(Winner15==7&generalElectionVoteW6!=7)

tab winconW6

// Controls //

*Turnout: 

tab genElecTurnoutRetroW6 
recode genElecTurnoutRetroW6 (9999=.)
rename genElecTurnoutRetroW6 turnoutW6
gen turnoutW4=0

*Ideology:

recode leftRightW4 leftRightW6 (9999=.)

gen ideologyW4=leftRightW4/10
gen ideologyW6=leftRightW6/10

*PID:

tab partyIdW4
recode partyIdW4 (9999=.)
rename partyIdW4 pidW4

tab partyIdW6
recode partyIdW6 (9999=.)
rename partyIdW6 pidW6

*Education:

recode educationW4 (16/18=1 "Degree") (1/15=0 "No degree") (19/20=0), gen (higheducW4)
recode educationW6 (16/18=1 "Degree") (1/15=0 "No degree") (19/20=0), gen (higheducW6)

*Age

tab ageW4
tab ageW6

*Ethnicity

tab p_ethnicityW4

recode p_ethnicityW4 (1=1 "White British") (else=0 "Other"), gen (ethnicW4)
recode p_ethnicityW6 (1=1 "White British") (else=0 "Other"), gen (ethnicW6)

*Gender

gen genderW4=.
replace genderW4 = 0 if gender==1 & wave4==1
replace genderW4 = 1 if gender==2 & wave4==1


gen genderW6=.
replace genderW6 = 0 if gender==1 & wave6==1
replace genderW6 = 1 if gender==2 & wave6==1

*Country:

tab countryW4
tab countryW6

*Region: 

tab gorW4
gen regionW4=.
replace regionW4=1 if gorW4==1|gorW4==2|gorW4==3|gorW4==4|gorW4==5|gorW4==6|gorW4==8|gorW4==9
replace regionW4=2 if gorW4==7
replace regionW4=3 if gorW4==10
replace regionW4=4 if gorW4==11

tab gorW6
gen regionW6=.
replace regionW6=1 if gorW6==1|gorW6==2|gorW6==3|gorW6==4|gorW6==5|gorW6==6|gorW6==8|gorW6==9
replace regionW6=2 if gorW6==7
replace regionW6=3 if gorW6==10
replace regionW6=4 if gorW6==11

tab regionW6

// Discussing politics

keep if affpol_W6!=. & affpol_W4!=.

*Recode so the numerical values for each party are the same: 

tab generalElectionVoteW4
tab discussantVoteName1W4
tab discussantVoteName2W4
tab discussantVoteName3W4

recode discussantVoteName1W4 (1=2) (2=1) (6=7) (7=6) (8/9999=.)
recode discussantVoteName2W4 (1=2) (2=1) (6=7) (7=6) (8/9999=.)
recode discussantVoteName3W4 (1=2) (2=1) (6=7) (7=6) (8/9999=.)

*Code heterogeneous discussion indicator:

*step-wise procedure. First code the heterogeneous discussion indicator as 0 if the discussant 1 is a co-partisan

gen DPhetero=0 if (generalElectionVoteW4==1 & discussantVoteName1W4==1)|(generalElectionVoteW4==2 & discussantVoteName1W4==2)|(generalElectionVoteW4==3 & discussantVoteName1W4==3)|(generalElectionVoteW4==4 & discussantVoteName1W4==4)|(generalElectionVoteW4==5 & discussantVoteName1W4==5)|(generalElectionVoteW4==6 & discussantVoteName1W4==6)|(generalElectionVoteW4==7 & discussantVoteName1W4==7)

*then code the discussion indicator as 1 if the respondent reports discussing politics with an out-partisan. This will recode all of those in the first step who were originally coded as co-partisan only, but are not. 

replace DPhetero=1 if (generalElectionVoteW4==1 & discussantVoteName1W4!=1 & discussantVoteName1W4!=.)|(generalElectionVoteW4==2 & discussantVoteName1W4!=2 & discussantVoteName1W4!=.)|(generalElectionVoteW4==3 & discussantVoteName1W4!=3 & discussantVoteName1W4!=.)|(generalElectionVoteW4==4 & discussantVoteName1W4!=4 & discussantVoteName1W4!=.)|(generalElectionVoteW4==5 & discussantVoteName1W4!=5 & discussantVoteName1W4!=.)|(generalElectionVoteW4==6 & discussantVoteName1W4!=6 & discussantVoteName1W4!=.)|(generalElectionVoteW4==7 & discussantVoteName1W4!=7 & discussantVoteName1W4!=.)

replace DPhetero=1 if (generalElectionVoteW4==1 & discussantVoteName1W4==1 & discussantVoteName2W4!=1 & discussantVoteName2W4!=.)|(generalElectionVoteW4==2 & discussantVoteName1W4==2 & discussantVoteName2W4!=2 & discussantVoteName2W4!=.)|(generalElectionVoteW4==3 & discussantVoteName1W4==3 & discussantVoteName2W4!=3 & discussantVoteName2W4!=.)|(generalElectionVoteW4==4 & discussantVoteName1W4==4 & discussantVoteName2W4!=4 & discussantVoteName2W4!=.)|(generalElectionVoteW4==5 & discussantVoteName1W4==5 & discussantVoteName2W4!=5 & discussantVoteName2W4!=.)|(generalElectionVoteW4==6 & discussantVoteName1W4==6 & discussantVoteName2W4!=6 & discussantVoteName2W4!=.)|(generalElectionVoteW4==7 & discussantVoteName1W4==7 & discussantVoteName2W4!=7 & discussantVoteName2W4!=.)

replace DPhetero=1 if (generalElectionVoteW4==1 & discussantVoteName1W4==1 & discussantVoteName2W4==1 & discussantVoteName3W4!=1 & discussantVoteName3W4!=.)|(generalElectionVoteW4==2 & discussantVoteName1W4==2 & discussantVoteName2W4==2 & discussantVoteName3W4!=2 & discussantVoteName3W4!=.)|(generalElectionVoteW4==3 & discussantVoteName1W4==3 & discussantVoteName2W4==3 & discussantVoteName3W4!=3 & discussantVoteName3W4!=.)|(generalElectionVoteW4==4 & discussantVoteName1W4==4 & discussantVoteName2W4==4 & discussantVoteName3W4!=4 & discussantVoteName3W4!=.)|(generalElectionVoteW4==5 & discussantVoteName1W4==5 & discussantVoteName2W4==5 & discussantVoteName3W4!=5 & discussantVoteName3W4!=.)|(generalElectionVoteW4==6 & discussantVoteName1W4==6 & discussantVoteName2W4==6 & discussantVoteName3W4!=6 & discussantVoteName3W4!=.)|(generalElectionVoteW4==7 & discussantVoteName1W4==7 & discussantVoteName2W4==7 & discussantVoteName3W4!=7 & discussantVoteName3W4!=.)

tab DPhetero

save initial.dta, replace


*Dropping non-study variables:

keep IP_anger_W* IP_fear_W* IP_hope_W* IP_pride_W* OP_anger_W* OP_fear_W* OP_pride_W* OP_hope_W* affpol_W4 affpol_W6 IP_like_W* OP_dislike_LLP_R_W* OP_dislike_MPV_R_W* ideopol_W* pidstrength* wingov* wincon* ideologyW* pidW* higheducW* ethnicW* genderW* ageW4 ageW6 countryW4 countryW6 id panoW6 regionW* partisan_mediaW* neutral_mediaW*  generalElectionVoteW6 DPhetero conAngryW* labAngryW* ldAngryW* snpAngryW* pcAngryW* ukipAngryW* grnAngryW* conPrideW* labPrideW* ldPrideW* snpPrideW* pcPrideW* ukipPrideW* grnPrideW* conFearW* labFearW* ldFearW* snpFearW* pcFearW* ukipFearW* grnFearW* conHopeW* labHopeW* ldHopeW* snpHopeW* pcHopeW* ukipHopeW* grnHopeW*

save modeldata_wide.dta, replace

**# Reshape long 

use modeldata_wide.dta, clear

tab ideologyW4
tab ideologyW6

gen ideoexAW4=(ideologyW4*2)-1
gen ideoexW4=sqrt((ideoexAW4^2))

tab ideoexAW4
tab ideoexW4

gen ideoexAW6=(ideologyW6*2)-1
gen ideoexW6=sqrt((ideoexAW6^2))

tab ideoexAW6
tab ideoexW6

keep if affpol_W6!=. & affpol_W4!=.

summ affpol_W6 affpol_W4

reshape long IP_anger_W IP_fear_W IP_hope_W IP_pride_W  OP_anger_W OP_fear_W OP_pride_W OP_hope_W affpol_W IP_like_MLP_W IP_like_W OP_dislike_LLP_W OP_dislike_LLP_R_W OP_dislike_MPL_R_W OP_dislike_MPV_R_W OP_dislike_CON_R_W ideopol_W pidstrengthW partisan_mediaW neutral_mediaW wingovW winconW turnoutW ideologyW ideoexW pidW higheducW ageW countryW genderW ethnicW regionW  conAngryW labAngryW ldAngryW snpAngryW pcAngryW ukipAngryW grnAngryW conPrideW labPrideW ldPrideW snpPrideW pcPrideW ukipPrideW grnPrideW conFearW labFearW ldFearW snpFearW pcFearW ukipFearW grnFearW conHopeW labHopeW ldHopeW snpHopeW pcHopeW ukipHopeW grnHopeW, i(id) j(wave)

xtset id wave

xtdes

browse

summ ideopol_W

gen ideopol_WRC=ideopol_W/4.906023

tab winconW

tab DPhetero
replace DPhetero=. if affpol_W==.

drop if OP_anger_W==.|affpol_W==.

save model_data.dta, replace


// Sample characteristics

*Main sample

xtdes

summ OP_anger_W
summ affpol_W
summ partisan_mediaW
summ neutral_mediaW
summ ideopol_WRC
summ ageW
tab genderW
tab higheducW
tab ethnicW
tab regionW

*Discussion sample

tab DPhetero

summ OP_anger_W if DPhetero!=.
summ affpol_W if DPhetero!=.
summ partisan_mediaW if DPhetero!=.
summ neutral_mediaW if DPhetero!=.
summ ideopol_WRC if DPhetero!=.
summ ageW if DPhetero!=.
tab genderW if DPhetero!=.
tab higheducW if DPhetero!=.
tab ethnicW if DPhetero!=.
tab regionW if DPhetero!=.


// Correlations 

*correlation matrix for anger, affective polarization and out-party dislike among the discussion group 

pwcorr OP_anger_W affpol_W  OP_dislike_LLP_R_W OP_dislike_MPV_R_W if DPhetero!=.


// Pre-election anger 

summ OP_anger_W if DPhetero==0 & wave==4
summ OP_anger_W if DPhetero==1 & wave==4

ttest OP_anger_W if wave==4, by(DPhetero) 

*t test is non-significant - p = 0.51

// Within-person variation in anger

use modeldata_wide.dta, clear

keep if OP_anger_W4!=.&OP_anger_W6!=.

bysort id: gen OP_A_change_sum=OP_anger_W6-OP_anger_W4

summ OP_A_change_sum if DPhetero==0
summ OP_A_change_sum if DPhetero==1

ttest OP_A_change_sum, by(DPhetero)


**# Fitting the models

use model_data.dta, clear

bysort id: egen BP_OP_anger=mean(OP_anger_W)
bysort id: gen WP_OP_anger=OP_anger_W-BP_OP_anger

summ BP_OP_anger 
summ WP_OP_anger

bysort id: egen BP_ideopol=mean(ideopol_WRC)
bysort id: gen WP_ideopol=ideopol_WRC-BP_ideopol

summ BP_ideopol
summ WP_ideopol

bysort id: egen BP_neutral_media=mean(neutral_mediaW)
bysort id: gen WP_neutral_media=neutral_mediaW-BP_neutral_media

summ BP_neutral_media
summ WP_neutral_media

bysort id: egen BP_partisan_media=mean(partisan_mediaW)
bysort id: gen WP_partisan_media=partisan_mediaW-BP_partisan_media

summ BP_partisan_media
summ WP_partisan_media

xtset id wave

*Then run a model to validate that WP coefficients correspond to twfe - they do: 

xtreg affpol_W OP_anger_W i.wave, fe vce(cluster id)

xtreg affpol_W WP_OP_anger BP_OP_anger i.wave, re vce(cluster id)

*Coding variables for interaction:

tab DPhetero

gen anger_int = OP_anger_W*DPhetero

bysort id: egen BP_anger_int=mean(anger_int)
bysort id: gen WP_anger_int=anger_int-BP_anger_int

summ BP_anger_int
summ WP_anger_int


// Affective polarization 

*fitting the model 

xtreg affpol_W WP_OP_anger WP_anger_int BP_OP_anger i.DPhetero i.higheducW i.genderW i.ethnicW i.regionW ageW WP_partisan_media BP_partisan_media WP_neutral_media BP_neutral_media WP_ideopol BP_ideopol i.wingovW i.winconW i.wave, re vce(cluster id)

test WP_anger_int

* Calculating marginal effects

lincom _b[WP_OP_anger]

lincom _b[WP_OP_anger] + _b[WP_anger_int]

* Manually create matrix of marginal effects for plotting: 

matrix input A = (0.341, -0.059 \ 0.142, 0.126)

* effect size 

summ affpol_W

* 1 sd = 0.97. coefficient = 0.34. 

* 0.34/0.97 = 0.35. So moving from the minimum to the maximum value on the out-party anger scale is associated with an increase of 0.35 standard deviations in affective polarization. 


// OP dislike - single 

* fitting the model 

xtreg OP_dislike_LLP_R_W WP_OP_anger WP_anger_int BP_OP_anger i.DPhetero i.higheducW i.genderW i.ethnicW i.regionW ageW WP_partisan_media BP_partisan_media WP_neutral_media BP_neutral_media WP_ideopol BP_ideopol i.wingovW i.winconW i.wave, re vce(cluster id)

test WP_anger_int

* Calculating marginal effects

lincom _b[WP_OP_anger]

lincom _b[WP_OP_anger] + _b[WP_anger_int]

* Manually create matrix of marginal effects for plotting: 

matrix input B = (0.211, -0.007 \ 0.214, 0.159)

* examining mean score for potential ceiling effects

summ OP_dislike_LLP_R_W if wave==4 & DPhetero!=.

// OP dislike - multi 

* fitting the model 

xtreg OP_dislike_MPV_R_W WP_OP_anger WP_anger_int BP_OP_anger i.DPhetero i.higheducW i.genderW i.ethnicW i.regionW ageW WP_partisan_media BP_partisan_media WP_neutral_media BP_neutral_media WP_ideopol BP_ideopol i.wingovW i.winconW i.wave, re vce(cluster id)

test WP_anger_int

* Calculating marginal effects

lincom _b[WP_OP_anger]

lincom _b[WP_OP_anger] + _b[WP_anger_int]


* Manually create matrix of marginal effects for plotting: 

matrix input C = (0.68, 0.129 \ 0.183, 0.14)

* effect size 

summ OP_dislike_MPV_R_W

* 1 sd = 1.2. coefficient = 0.68

* 0.68/1.2 = 0.57. 
* 0.57/5 = 0.11

// IP like 

* fitting the model 

xtreg IP_like_W WP_OP_anger WP_anger_int BP_OP_anger i.DPhetero i.higheducW i.genderW i.ethnicW i.regionW ageW WP_partisan_media BP_partisan_media WP_neutral_media BP_neutral_media WP_ideopol BP_ideopol i.wingovW i.winconW i.wave, re vce(cluster id)

test WP_anger_int

* Calculating marginal effects

lincom _b[WP_OP_anger]

lincom _b[WP_OP_anger] + _b[WP_anger_int]

* Manually create matrix of marginal effects for plotting: 

matrix input D = (-0.109, -0.07 \ 0.328, 0.238)

// coefplot 

coefplot (mat(A), se(2) xline(0) msym(O) mcolor(gs1) ciopts(color(gs1))) || (mat(B), se(2) msym(O) mcolor(gs1) ciopts(color(gs1))) || (mat(C), se(2) xline(0) msym(O) mcolor(gs1) ciopts(color(gs1))) || (mat(D), se(2) msym(O) mcolor(gs1) ciopts(color(gs1))), ///
coeflabels(c1 = "Co-partisan discussion" c2 = "Out-partisan discussion") ///
byopts(compact cols(1)) 


**# Robustness checks:

// Alternative emotions 

*decomposing OP fear variable:

  bysort id: egen BP_OP_fear=mean(OP_fear_W)
bysort id: gen WP_OP_fear=OP_fear_W-BP_OP_fear

summ BP_OP_fear 
summ WP_OP_fear

*decomposing IP pride variable 

 bysort id: egen BP_IP_pride=mean(IP_pride_W)
bysort id: gen WP_IP_pride=IP_pride_W-BP_IP_pride

summ BP_IP_pride
summ WP_IP_pride

*decomposing IP hope variable 

 bysort id: egen BP_IP_hope=mean(IP_hope_W)
bysort id: gen WP_IP_hope=IP_hope_W-BP_IP_hope

summ BP_IP_hope
summ WP_IP_hope

xtreg affpol_W WP_OP_anger WP_anger_int BP_OP_anger WP_OP_fear BP_OP_fear WP_IP_pride BP_IP_pride WP_IP_hope BP_IP_hope i.DPhetero i.higheducW i.genderW i.ethnicW i.regionW ageW WP_partisan_media BP_partisan_media WP_neutral_media BP_neutral_media WP_ideopol BP_ideopol i.wingovW i.winconW i.wave, re vce(cluster id)

// EVT model

*decomposing IP fear variable:

bysort id: egen BP_IP_fear=mean(IP_fear_W)
bysort id: gen WP_IP_fear=IP_fear_W-BP_IP_fear

summ BP_IP_fear
summ WP_IP_fear


*decomposing IP anger variable:

bysort id: egen BP_IP_anger=mean(IP_anger_W)
bysort id: gen WP_IP_anger=IP_anger_W-BP_IP_anger

summ BP_IP_anger
summ WP_IP_anger

*decomposing OP pride variable:

  bysort id: egen BP_OP_pride=mean(OP_pride_W)
bysort id: gen WP_OP_pride=OP_pride_W-BP_OP_pride

summ BP_OP_pride 
summ WP_OP_pride


*decomposing OP hope variable:

bysort id: egen BP_OP_hope=mean(OP_hope_W)
bysort id: gen WP_OP_hope=OP_hope_W-BP_OP_hope

summ BP_OP_hope 
summ WP_OP_hope


xtreg affpol_W WP_OP_anger WP_anger_int BP_OP_anger  WP_IP_anger BP_IP_anger WP_OP_fear BP_OP_fear WP_IP_fear BP_IP_fear WP_IP_pride BP_IP_pride WP_OP_pride BP_OP_pride WP_IP_hope BP_IP_hope WP_OP_hope BP_OP_hope i.DPhetero i.higheducW i.genderW i.ethnicW i.regionW ageW WP_partisan_media BP_partisan_media WP_neutral_media BP_neutral_media WP_ideopol BP_ideopol i.wingovW i.winconW i.wave, re vce(cluster id)

// Model with pid strength: 

bysort id: egen BP_pidstrength=mean(pidstrengthW)
bysort id: gen WP_pidstrength=pidstrengthW-BP_pidstrength

summ BP_pidstrength
summ WP_pidstrength

xtreg affpol_W WP_OP_anger WP_anger_int BP_OP_anger i.DPhetero i.higheducW i.genderW i.ethnicW i.regionW ageW WP_partisan_media BP_partisan_media WP_neutral_media BP_neutral_media WP_ideopol BP_ideopol WP_pidstrength BP_pidstrength i.wingovW i.winconW i.wave, re vce(cluster id)

test WP_anger_int

// CLPMS

use modeldata_wide.dta, clear

keep if OP_anger_W4!=.&OP_anger_W6!=.&affpol_W4!=.&affpol_W6!=.


*creating standardized variables 

egen affpol_W4_std = std(affpol_W4)
egen OP_anger_W4_std = std(OP_anger_W4)

egen affpol_W6_std = std(affpol_W6)
egen OP_anger_W6_std = std(OP_anger_W6)

*running the model 

reg OP_anger_W6_std affpol_W4_std OP_anger_W4_std
reg affpol_W6_std affpol_W4_std OP_anger_W4_std





